<template>
  <div>
    <!--本体列表，数据 -->
    <div class="table-body-box">
      <div class="mb-10 d-flex a-items">
        <component
            v-bind:is="item.propertyType"
            :property.sync="item"
            v-for="(item, index) in getPower"
            :key="index"
            @updateVal="updateVal"
        ></component>
        <el-select v-model="editForm.is_examine" placeholder="申请状态" size="small"
                   class="w-140 mr-10">
          <el-option :label="item.name" :value="item.id" v-for="(item, index) in statusList"
                     :key="index"></el-option>
        </el-select>
        <el-button type="primary" @click="searchBtn" size="small"><i class="el-icon-search"></i>搜索</el-button>
        <el-button size="small" @click="refreshFn"><i class="el-icon-refresh"></i>刷新</el-button>
        <el-button size="small" type="primary" plain @click="openBorrwBook(1)"
                   :disabled="!editForm.is_examine&&handleSelection.length?false:true">批量确认
        </el-button>
        <el-button size="small" type="warning" plain @click="openBorrwBook(2)"
                   :disabled="!editForm.is_examine&&handleSelection.length?false:true">批量驳回
        </el-button>
      </div>
      <el-table
          ref="multipleTable"
          :data="arryList"
          :stripe="tableStyle.tableStripe"
          :border="tableStyle.tableBorder"
          tooltip-effect="dark"
          style="width: 100%"
          header-cell-class-name="table-text-c"
          cell-class-name="table-text-c"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="50"
            align="center">
        </el-table-column>
        <el-table-column
            label="返库教具"
            align="center">
          <template slot-scope="scope">
            {{ scope.row.tools ? scope.row.tools.name : '未知' }}
          </template>
        </el-table-column>
        <el-table-column
            prop="number"
            label="返库数量"
            width="85"
            align="center">
        </el-table-column>
        <el-table-column
            prop="create_time"
            label="申请时间"
            align="center">
        </el-table-column>
        <el-table-column
            label="申请班级"
            align="center"
            key="2">
          <template slot-scope="scope">
            {{ scope.row.classes ? scope.row.classes.class_name : '' }}
          </template>
        </el-table-column>
        <el-table-column
            label="申请老师"
            align="center"
            key="3">
          <template slot-scope="scope">
            {{ scope.row.user.realname }}
          </template>
        </el-table-column>
        <el-table-column
            label="状态"
            align="center"
            width="120"
            key="6">
          <template slot-scope="scope">
            <span v-if="scope.row.is_examine==0" class="c-danger">待确认</span>
            <span v-if="scope.row.is_examine==1" class="c-success">已确认</span>
            <span v-if="scope.row.is_examine==2" class="c-warning">已驳回</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          background
          layout="total, prev, pager, next"
          :total="page.total"
          :page-size="page.pagesize"
          @current-change="handleSizeChange">
      </el-pagination>
    </el-col>
    <detailApply :visible.sync="showPush"></detailApply>
  </div>
</template>

<script>
// import download from '@/components/common/download'
import {pagination, tableoption} from '@/utils/util.js'
import detailApply from "./detailApply";
import {tools_return_index, tools_return_distribution, tools_return_reject} from "@/api/admin/teaching"

export default {
  components: {
    detailApply
  },
  data() {
    return {
      arryList: [],
      showPush: false,
      tableStyle: tableoption,
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      statusList: [
        {
          id: 99,
          name: '全部'
        },
        {
          id: 0,
          name: '未处理'
        },
        {
          id: 1,
          name: '已确认'
        },
        {
          id: 2,
          name: '已驳回'
        }
      ],
      editForm: {
        section_id: '',
        class_id: '',
        p: '',
        is_examine: 0
      }, // 编辑界面数据
      // 预览
      imgsrcCheck: '',
      dialogImgVisible: false,
      cateCount: '',
      cateSum: '',
      classLsit: [],
      handleSelection: [],
      getPower: {
        'section_id': {
          name: 'section_id', // key名称
          default: '', // 默认值
          propertyType: "Select", // 组件名称
          placeholder: '年段', // 默认值
          valueText: '', // 值
          leve: 2,
          api: this.api.user_sectionindex, // 下拉框的请求接口
          keyName: 'section_name',// 调用的字段
          child: [
            {
              name: 'class_id',
              default: '',
              propertyType: "",
              placeholder: '班级',
              valueText: '',
              api: this.api.user_classesindex,
              keyName: 'class_name',
            }
          ]
        }
      },
      statusTearchList: [
        {
          value: 0,
          label: '申请中'
        },
        {
          value: 1,
          label: '完成申请'
        }
      ],
    }
  },
  created() {
    // // 获取当前老师班级
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      let data_ = {
        is_examine: this.editForm.is_examine===99?'':this.editForm.is_examine,
        p: this.page.pageindex,
        section_id: this.editForm.section_id,
        class_id: this.editForm.class_id
      }
      tools_return_index(data_).then(res => {
        console.log('res:', res)
        this.arryList = res.list.data
        this.page.total = res.list.total
      })
    },
    // 搜索
    searchBtn() {
      this.page.pageindex = 1
      this.InitFn()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    viewImgDShow(path) {
      this.imgsrcCheck = path
      this.dialogImgVisible = true
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 刷新页面
    refreshFn() {
      this.editForm.section_id = ''
      this.editForm.class_id = ''
      let newGetPower = this.getJson(this.getPower)
      newGetPower['section_id'].valueText = '';
      newGetPower['section_id'].child[0].valueText = '';
      this.getPower = newGetPower
      this.page.pageindex = 1
      this.InitFn()
    },
    openBorrwBook(type, index) {
      let that = this
      let data_id = []
      for (let item of this.handleSelection) {
        data_id.push(item.id)
      }
      // tools_return_distribution, tools_return_reject
      // console.log('data_id:', data_id)
      // let state = 1
      // if(state){
      //   return
      // }
      this.$confirm(`确认${type === 1 ? '通过' : '驳回'}该返库申请，是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (type === 1) {
          tools_return_distribution({id: data_id.join(',')}).then(res => {
            console.log('res:', res)
            if (!res.code) {
              that.InitFn()
            }
          })
        } else {
          tools_return_reject({id: data_id.join(',')}).then(res => {
            console.log('res:', res)
            if (!res.code) {
              that.InitFn()
            }
          })
        }
      }).catch(() => {
      })
    },
    async checkine(data_) {
      this.$post(api.admin_materiel_log_sell, data_).then((response) => {
        if (response.code === 0) {
          this.InitFn()
          this.$message.success(data.msg)
        } else {
          console.log('请求失败')
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    handleSelectionChange(val) {
      console.log('选择框选择变化', val) // 所有选中值
      this.handleSelection = val
    },
    updateVal(option) {
      // console.log('变更数据收到的数据内容：', option)
      if (option.input) {
        this.editForm[option['input'].name] = option['input'].valueText
      } else if (option.select) {
        for (let i in option.select) {
          if (option.select[i].name) {
            this.editForm[option.select[i].name] = option.select[i].valueText
          } else {
            this.editForm[option.select[i - 1].name] = option.select[i].valueText ? option.select[i].valueText : option.select[i - 1].valueText
          }
        }
      }
    },
  },
  mounted() {
  }
}
</script>


